home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4103 < prev    next >
Encoding:
Text File  |  1996-08-06  |  2.6 KB  |  80 lines

  1. Path: news.algonet.se!usenet
  2. From: bjorn@algonet.se (Bjorn Fahller)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Callbacks using member functions
  5. Date: Sun, 28 Jan 1996 01:09:48 +0100
  6. Organization: K-os
  7. Message-ID: <M7rCxsNapkOO089yn@algonet.se>
  8. References: <4dgkke$8mf@knot.queensu.ca>
  9. NNTP-Posting-Host: sophocles.algonet.se
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=ISO-8859-1
  12. Content-Transfer-Encoding: 8bit
  13.  
  14. In article <4dgkke$8mf@knot.queensu.ca>,
  15. 3mal5@qlink.queensu.ca (Lepage Marc A) wrote:
  16.  
  17. [asking about methods as callbacks]
  18.  
  19. Quite a few have pointed out why it is not possible to have a
  20. method (that is not static) as a callback.
  21.  
  22. As every so often, I'm going in the other direction, and say that
  23. I often do what you want to do. It's not always that easy thought,
  24. and in your case, you may need to write a wrapper class for
  25. Motif (by the way, Rogue Wave's View.h++ is nice, although
  26. it defeats type checking with its method based callbacks.)
  27.  
  28. A while ago, almost a year ago, there was an article in "C++ Report"
  29. about a template based callback mechanism, that cared little if the
  30. callback was a function, or the combination of an object and a method.
  31.  
  32. The solution looked great on the surface, but hid some rather naughty
  33. things when examining it closer (It was very cleverly thougth out,
  34. though, and I use it a lot at home, since using it is so clean!)
  35.  
  36. All in all, what you need to do, is to tie both a method and
  37. a pointer to an object, as the callback. The only way I can see
  38. to do that, with your Motif callback, is to write a class (or function
  39. combo) like this:
  40.  
  41. template <class Callback>
  42. class callback
  43. {
  44. public:
  45.   static void set(Callback*, RetType (Callback*)::method(ArgTypes));
  46.   static RetType cb(ArgTypes);
  47. private:
  48.   static RetType (Callback*)::m(ArgTypes);
  49.   static Callback* p;
  50. }
  51.  
  52. template <class Callback>
  53. void callback<Callback>::set(Callback* po, RetType (Callback*)::method(ArgTypes))
  54. {
  55.   p = po;
  56.   m = method;
  57.   // do whatever to register callback::cb as the callback function.
  58. }
  59.  
  60. template <class Callback>
  61. RetType callback<Callback>::cb(ArgTypes args)
  62. {
  63.   return (p->m)(args);
  64. }
  65.  
  66. Where RetType is the required type of the callback function,
  67. and ArgTypes is the type of the argument (or several, if so is
  68. needed.)
  69.  
  70. Hope this helps.
  71.    _
  72. /Bjorn. (PS, the example above is not tested, so errors are more
  73.          likely than not.)
  74. --
  75.  Bjorn Fahller             | Phone W: +46 8 4220898 | General Purpose Guru
  76.  Siljansvagen 35           |       H: +46 8 918297  | and Problem Solver
  77.  S-120 55 Arsta/SWEDEN     | Finger me for PGP Key  | Extraordinaire
  78.  PGP Key fingerprint =  7E 3D 1B E2 3F D4 E8 0C  E2 82 AB 81 E4 18 BF 39
  79.  
  80.